  <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        body{
            background-image: url("catimg/maomi4.jpg");
            background-size: cover;
            background-repeat: no-repeat;
            text-align: center;
        }
        img{
            width: 300px;
        }
    </style>
</head>
<body>
<div id="app">
    <img src="catimg/regcat.png" alt="">
    <el-card style="width: 480px;height:320px; background-color: rgba(80,153,255,0.3);
     margin: 0 auto;border:0" >
        <div slot="header" style=" font-size: 35px;color: #f09117; margin: 0">
            <span>欢迎登录街巷的猫</span>
            <el-form label-width="40px" style="width: 400px;margin: 20px auto;
        color:black;font-weight:bold">
                <el-form-item label="账号" >
                    <el-input type="text" v-model="user.username" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input type="password" v-model="user.password" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-checkbox v-model="user.rem">记住用户名和密码</el-checkbox>
                </el-form-item>
                <el-form-item>
                    <el-button style="position: relative;left: -20px" type="primary" @click="login()">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-card>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                user:{username:"",password:"",rem:false}
            }
        },
        methods: {
            login() {//发出axios登录请求
                console.log(v.user)
                axios.post("/login", v.user).then(function (response) {
                    if (response.data == 1) {
                        alert("登录成功!");
                        location.href = "/index.html";
                    } else if(response.data ==3) {
                        v.$message.error("用户名错误");
                    }else {
                        v.$message.error("密码错误");
                    }
                })
            }
        },
        created:function () {
            //  username=admin; password=123456
            let arr = document.cookie.split(";");
            let count = 0;
            //遍历
            for (let cookieStr of arr) {
                let cookieArr = cookieStr.split("=");
                let key = cookieArr[0].trim();
                let value = cookieArr[1];
                if (key=='username'){
                    //如果在Vue对象初始化过程中访问对象时不能使用v 需要使用this
                    //如果此处发出异步请求后访问Vue对象 不能使用this必须使用v
                    this.user.username = value;
                    count++
                }else if (key=='password'){
                    this.user.password = value;
                    count++
                }
            }
            this.user.rem = count==2;
        }
    })
</script>
</html>
